package com.compass.doctor.ui.patient

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.databinding.ActivityNewPatientBinding
import com.compass.doctor.ui.patient.adapter.NewPatientAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 新的患者
 * Date:2023/12/14 11:26
 */

class NewPatientActivity : BaseVBActivity<ActivityNewPatientBinding>(), OnRefreshListener{

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, NewPatientActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val patientViewModel: PatientViewModel by viewModels()
    private lateinit var mAdapter: NewPatientAdapter

    override fun initView(savedInstanceState: Bundle?) {
        initRecyclerView()
    }

    override fun initData() {
        showLoading()
        getNewPatientList()
        patientViewModel.newPatientLiveData.observe(this) {
            dismissLoading()
            mBinding.refreshLayout.finishRefresh()
            it?.let { list->
                mBinding.emptyView.gone()
                mAdapter.setData(list)
            } ?:kotlin.run {
                mBinding.emptyView.visible()
            }

        }
    }

    /**
     * 获取推荐列表数据
     */
    private fun getNewPatientList() {
        patientViewModel.getNewPatientList()
    }


    private fun initRecyclerView() {
        mBinding.refreshLayout.apply {
            setEnableRefresh(true)
            setEnableLoadMore(false)
            setOnRefreshListener(this@NewPatientActivity)

        }
        mAdapter = NewPatientAdapter(this)
        val dp12 = dpToPx(12)
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(left = dp12, top = dp12, right = dp12, bottom = dp12)
                setLastBottom(true)

            })
            adapter = mAdapter
        }



    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getNewPatientList()
    }


}